#ifndef EXC_SCREEN_H
#define EXC_SCREEN_H

#include "sql_mcdefine.h"
#include "sql_mcrecorder.h"
#include "ExcRecordDisplayer.h"

#include <QThread>
#include <QQuickWidget>
#include <QLocalSocket>
#include <QByteArray>

class ExcQmlModbusInterface;
struct _ExcLsBufStruct{
    QVector<quint16> vec;
    QDateTime time;
};

class ExcScreen: public QThread
{
    Q_OBJECT
public:
    ExcScreen();
signals:
    void SN_connectToLocalSocketFault();

private:
    Sql_MCDefine *sql_mcDefine;
    Sql_MCRecorder *sql_mcRecorder;
    QQuickWidget *m_quickWidget;
    ExcRecordDisplayer *m_excRecordDisplayer;
    ExcQmlModbusInterface *m_excQmlModbusInterface;
};

class ExcQmlModbusInterface: public QThread
{
    Q_OBJECT
public:
    ExcQmlModbusInterface(Sql_MCRecorder *sql):sql_mcRecorder(sql)
    {
        start();
    }

    QLocalSocket *m_localSocket;
    QMutex m_mutexLocalSocketRecQueue;
    QQueue<struct _ExcLsBufStruct> m_localSocketRecQueue;


public slots:
    //qml调用
    void sl_modbusWriteImpulse(int i_addr){

#ifdef _CPP_DEBUG_PRINT_
        qDebug()<<i_addr;
#endif
        QByteArray t_byar;
        t_byar.resize(5);
        t_byar[0]=0x0;
        t_byar[1]=i_addr&0xFF;
        t_byar[2]=(i_addr>>8)&0xFF;
        t_byar[3]=0x0;
        t_byar[4]=0x0;
        m_localSocket->write(t_byar);
    }

    void sl_modbusWriteData(int i_addr, int i_data){
#ifdef _CPP_DEBUG_PRINT_
        qDebug()<<i_addr;
#endif
        QByteArray t_byar;
        t_byar.resize(5);
        t_byar[0]=0x1;
        t_byar[1]=i_addr&0xFF;
        t_byar[2]=(i_addr>>8)&0xFF;
        t_byar[3]=i_data&0xFF;
        t_byar[4]=(i_data>>8)&0xFF;
        m_localSocket->write(t_byar);
    }

    void sl_modbusWriteDataArray(int i_addr, QVector<int> i_data, int counter){

#ifdef _CPP_DEBUG_PRINT_
        qDebug()<<i_addr;
#endif
        QByteArray t_byar;
        if(counter<2) return;
        t_byar.resize(3+2*counter);
        t_byar[0]=counter;
        t_byar[1]=i_addr&0xFF;
        t_byar[2]=(i_addr>>8)&0xFF;

        for(int i =0; i<counter; i++)
        {
            t_byar[3+2*i] = i_data.at(i)&0xFF;
            t_byar[4+2*i] = (i_data.at(i)>>8)&0xFF;
        }
        m_localSocket->write(t_byar);
    }

private:
    Sql_MCRecorder *sql_mcRecorder;

    void run() override;

signals:
    void SN_close();
};


#endif // EXC_SCREEN_H
